package com.mrx.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication(scanBasePackages = {"com.mrx"})
public class HelloWorldApplication {

    private static final Logger logger = LoggerFactory.getLogger(HelloWorldApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class);
    }

    @EventListener(ApplicationReadyEvent.class)
    public void ready() {
        logger.info("application Ready");
        ApplicationContext applicationContext = ApplicationUtils.applicationUtils.context;
        logger.info("controllers: {}", applicationContext.getBeansWithAnnotation(RestController.class));
    }

    @Configuration
    public static class ApplicationUtils implements ApplicationContextAware {

        public ApplicationContext context;

        public static ApplicationUtils applicationUtils;

        private static final Logger logger = LoggerFactory.getLogger(ApplicationUtils.class);

        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            context = applicationContext;
            applicationUtils = this;
            logger.info("init ApplicationUtils");
        }

    }

}
